<template>
  <div>
    <van-nav-bar title="城市切换" left-arrow @click-left="leftBack" />
    <div class="city-switch">
      <h3>当前城市：</h3>
      <div class="city-switch-text">
        <span>{{store.cityValue}}</span>
      </div>
      <h3>城市切换：</h3>
      <div class="city-switch-text">
        <span @click="closeCitySwitch(item.name)" v-for="(item, index) in store.cityList.list" :key="index">{{
          item.name
        }}</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { cityList } from "@/api/task.ts";
import { showToast } from "vant";
import { reactive,inject } from "vue";
import { taskStore } from "../../../store/task.ts";

// 声明inject
const {closeCitySwitch} = inject('popup')

const leftBack = () => closeCitySwitch()
const state = reactive({
  htmlText: "",
});
// 引用pinia的store
const store = taskStore();
const getCityList = async () => {
  const res = await cityList();
  if (res) {
    store.setCityList(res);
  } else {
    showToast(res.msg);
  }
};
if(store.cityList.length<=0){
  getCityList();
}
</script>

<style lang="scss" scoped>
.city-switch {
  padding: 0 0.7rem;
  h3 {
    font-size: 0.8rem;
    line-height: 0.8rem;
    font-weight: 500;
    color: #000;
    margin: 0.7rem 0 0.96rem;
  }
  &-text {
    span {
      width: 4.01rem;
      height: 1.6rem;
      background: #fff;
      border: 1px solid #ebebeb;
      border-radius: 0.11rem;
      font-size: 0.64rem;
      text-align: center;
      font-weight: 400;
      color: #333;
      margin: 0 0.7rem 0.8rem 0;
      display: inline-block;
      line-height: 1.6rem;
      &:nth-child(4n) {
        margin-right: 0px;
      }
    }
  }
}
</style>